/*
 * Copyright 2011 Raimon Rodriguez
 *
 * This file is part of sgfx.
 *
 * sgfx is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * sgfx is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with sgfx. If not, see <http://www.gnu.org/licenses/>.
 */
package com.googlecode.sgfx.base;

import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Raimon Rodriguez
 */
public final class Log {
    private final Logger delegate;


    private Log(Logger pDelegateLog) {
        delegate = pDelegateLog;
    }

    
    public static Log getLog(Class<?> pClass) {
        // TODO: do not create new instance every time.
        return new Log(Logger.getLogger(pClass.getName()));
    }

    public void warning(String pMsgFormat, Object... pFormatArgs) {
        if (delegate.isLoggable(Level.WARNING)) {
	    logPrecise(Level.WARNING, pMsgFormat, pFormatArgs);
	}
    }

    public void info(String pMsgFormat, Object... pFormatArgs) {
        if (delegate.isLoggable(Level.INFO)) {
	    logPrecise(Level.INFO, pMsgFormat, pFormatArgs);
	}
    }

    public void fine(String pMsgFormat, Object... pFormatArgs) {
        if (delegate.isLoggable(Level.FINE)) {
	    logPrecise(Level.FINE, pMsgFormat, pFormatArgs);
	}
    }

    public void finer(String pMsgFormat, Object... pFormatArgs) {
        if (delegate.isLoggable(Level.FINER)) {
            logPrecise(Level.FINER, pMsgFormat, pFormatArgs);
	}
    }

    public void finest(String pMsgFormat, Object... pFormatArgs) {
        if (delegate.isLoggable(Level.FINEST)) {
            logPrecise(Level.FINEST, pMsgFormat, pFormatArgs);
	}
    }


    private void logPrecise(Level pLevel, String pMsgFormat, Object... pFormatArgs) {
        StackTraceElement sourceStackTraceElement = new Throwable().getStackTrace()[2];
        delegate.logp(pLevel, sourceStackTraceElement.getClassName(),
                sourceStackTraceElement.getMethodName(), String.format(pMsgFormat, pFormatArgs));
    }
}
